package com.pc.web.boardQnA;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pc.web.board.PageBeans;

@Controller
public class BoardQnAController {
	@Autowired BoardQnADAO dao;
	@Autowired BoardQnAService service;
		
	//글쓰기 폼 페이지
	@RequestMapping(value = "/boardQnA/writeForm", method = RequestMethod.GET)
	public String writeFormpage(){
		return "/boardQnA/boardQnA_writeForm";
	}
	//회원 글쓰기 페이지
	@RequestMapping(value = "/boardQnA/write", method = RequestMethod.POST)
	public String writePage(HttpServletRequest request, PageQnABeans Pbeans, BoardQnABeans beans,Model model){
		//클라이언트 ip추적
		String boardQnA_ip=request.getRemoteAddr();
		beans.setBoardQnA_ip(boardQnA_ip);
		dao.insert(beans);
		service.listpage(beans, Pbeans, model);
		return "/boardQnA/boardQnA_list";
	}
	//비밀번호 확인
	@RequestMapping(value = "/boardQnA/check", method = {RequestMethod.GET, RequestMethod.POST})
	public String confirmPW(BoardQnABeans beans, Model model){
		model.addAttribute("beans", beans);
		return "/boardQnA/boardQnA_checkPW";
	}
	//글 리스트 페이지
	@RequestMapping(value = "/boardQnA/list", method = {RequestMethod.GET, RequestMethod.POST})
	public String listPage(BoardQnABeans beans, PageQnABeans Pbeans, Model model){
		service.listpage(beans, Pbeans, model);
		return "/boardQnA/boardQnA_list";
	}
	
	//글 상세보기 페이지
	@RequestMapping(value = "/boardQnA/detail", method = {RequestMethod.GET, RequestMethod.POST})
	public String detailViewPage(String boardQnA_no, BoardQnABeans beans, HttpSession session, Model model){
		dao.countUpdate(boardQnA_no);
		service.view(boardQnA_no, beans, model);
		return "/boardQnA/boardQnA_detailView";
	}
	
	//글 수정상세 페이지
	@RequestMapping(value = "/boardQnA/modifyView", method = {RequestMethod.GET, RequestMethod.POST})
	public String modifyViewPage(String boardQnA_no, BoardQnABeans beans, Model model){
		beans=dao.modifyView(boardQnA_no);
		model.addAttribute("beans", beans);
		return "/boardQnA/boardQnA_modifyView";
	}
	
	//글 수정 페이지
	@RequestMapping(value = "/boardQnA/modify", method = RequestMethod.POST)
	public String modifyPage(String boardQnA_no, BoardQnABeans beans, PageQnABeans Pbeans, Model model){
		dao.update(beans);
		model.addAttribute("beans", beans);
		service.listpage(beans, Pbeans, model);
		return "/boardQnA/boardQnA_list";
	}

	//글 삭제 페이지
	@RequestMapping(value = "/boardQnA/delete", method = RequestMethod.GET)
	public String deletePage(String boardQnA_no, BoardQnABeans beans, PageQnABeans Pbeans, Model model){
		dao.delete(boardQnA_no);
		service.listpage(beans, Pbeans, model);
		return "/boardQnA/boardQnA_list";
	}
	
	//댓글 삭제 페이지
	@RequestMapping(value = "/boardQnA/commentDelete", method = RequestMethod.POST)
	public String commentDelete(String boardQnA_no, String boardQnA_number, BoardQnABeans beans, PageBeans Pbeans, Model model){
		dao.commentDelete(boardQnA_number);
		dao.commentList(boardQnA_no);
		service.view(boardQnA_no, beans, model);
		return "/boardQnA/boardQnA_detailView";
	}

	//댓글 입력 및 리스트
	@RequestMapping(value = "/boardQnA/commentInsert", method = RequestMethod.POST)
	public String commentInsert(String boardQnA_number, String boardQnA_no, BoardQnABeans beans, Model model){
		dao.commentInsert(beans);
		service.view(boardQnA_no, beans, model);
		return "/boardQnA/boardQnA_detailView";
	}
}